package com.sky.aspect;


import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自动记录操作日志
 *
 * @Author itcast
 * @Create 2023/4/25
 **/
@Aspect
@Component
@Slf4j
public class AutoLogAspect {

    /**
     * 切入点
     */
    @Pointcut("execution(* com.sky.service.*.*(..)) && @annotation(com.sky.annotation.Log)")
    public void autoLogPointCut(){}

    /**
     * 通知 为公共属性赋值
     */
    @Around("autoLogPointCut()")
    public Object autoFillAdvice(ProceedingJoinPoint joinPoint){
        log.info("自动记录操作日志...");

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String name = signature.getMethod().getName();//被拦截的方法名

        long begin = System.currentTimeMillis();

        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        long end = System.currentTimeMillis();

        log.info("被拦截的方法为：{},业务耗时：{}", name,end - begin);
        return proceed;
    }
}